Sort(List<MedicalViewerImageData>) Method

Summary
Sorts the input list of data based on various values stored inside them.
Syntax
C#
C++/CLI
public bool Sort( 
   List<MedicalViewerImageData> inputList 
) 
            public: 
bool Sort(  
   List<MedicalViewerImageData^>^ inputList 
)  

Parameters

inputList
a list of MedicalViewerImageData that will be stored.

Remarks
  • The method will sort the list based on

    • Image position
    • image orientation
    • echo number
    • instance number
    • frame of reference UID

    so it is important for a successful sorting, to have those data properly filled in.

  • if the sort failed for any reason, it will return false.

Example
C#
using Leadtools; 
using Leadtools.Dicom; 
using Leadtools.Medical3D; 
using Leadtools.Codecs; 
using Leadtools.MedicalViewer; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Core; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Annotations.Engine; 
using Leadtools.Annotations.Designers; 
 
class MedicalViewerSeriesManagerFrom : Form 
{ 
   DicomElement _studyElement; 
   DicomElement _seriesElement; 
   MedicalViewerSeriesManager _seriesManager; 
   MedicalViewerSeriesManager _seriesManager512; 
   List<MedicalViewerImageData> _imageDataList; 
   double[] doubleArray; 
   DicomElement patientElement; 
   string referenceUID; 
   DicomElement imageElement; 
   MedicalViewer _medicalViewer; 
 
   public MedicalViewerSeriesManagerFrom() 
   { 
 
   } 
 
   void MedicalViewerSeriesManagerFrom_SizeChanged(object sender, EventArgs e) 
   { 
      _medicalViewer.Size = new Size(this.ClientRectangle.Right, this.ClientRectangle.Bottom); 
   } 
 
   public MedicalViewerSeriesManagerFrom(MedicalViewerSeriesManager output) 
   { 
      RasterCodecs _codecs = new RasterCodecs(); 
 
      this.SizeChanged += new EventHandler(MedicalViewerSeriesManagerFrom_SizeChanged); 
 
      // Create the medical viewer and adjust the size and the location. 
      _medicalViewer = new MedicalViewer(1, 2); 
      _medicalViewer.Location = new Point(0, 0); 
      _medicalViewer.Size = new Size(this.ClientRectangle.Right, this.ClientRectangle.Bottom); 
 
      _seriesManager = output; 
 
      MedicalViewerMultiCell cell = new MedicalViewerMultiCell(null, true, 1, 1); 
 
      int index; 
      int count = output.Stacks[0].Items.Count; 
      CodecsImageInfo codecsInformation; 
 
      MedicalViewerImageInformation[] imageInformation = new MedicalViewerImageInformation[count]; 
 
      for (index = 0; index < count; index++) 
      { 
         codecsInformation = _codecs.GetInformation((string)(output.Stacks[0].Items[index].Data), true); 
 
         imageInformation[index] = new MedicalViewerImageInformation(); 
         imageInformation[index].ImageHeight = codecsInformation.Width; 
         imageInformation[index].ImageWidth = codecsInformation.Width; 
         imageInformation[index].XResolution = codecsInformation.XResolution; 
         imageInformation[index].YResolution = codecsInformation.YResolution; 
      } 
 
      cell.FramesRequested += new EventHandler<MedicalViewerRequestedFramesInformationEventArgs>(cell_FramesRequested); 
      FormClosing += new FormClosingEventHandler(MedicalViewerSeriesManagerFrom_FormClosing); 
      cell.EnableLowMemoryUsage(2, count, imageInformation); 
 
      _medicalViewer.Cells.Add(cell); 
 
 
      cell.PixelSpacing = output.Stacks[0].PixelSpacing; 
      cell.ImageOrientation = output.Stacks[0].Items[0].ImageOrientationArray; 
      cell.FrameOfReferenceUID = output.Stacks[0].Items[0].FrameOfReferenceUID; 
 
      for (index = 0; index < count; index++) 
      { 
         cell.SetImagePosition(index, output.Stacks[0].Items[index].ImagePosition, (index == count - 1)); 
      } 
 
 
      // add some actions that will be used to change the properties of the images inside the control. 
      cell.AddAction(MedicalViewerActionType.WindowLevel); 
      cell.AddAction(MedicalViewerActionType.Alpha); 
      cell.AddAction(MedicalViewerActionType.Offset); 
      cell.AddAction(MedicalViewerActionType.Stack); 
 
      // assign the added actions to a mouse button, meaning that when the user clicks and drags the mouse button, the associated action will be activated. 
      cell.SetAction(MedicalViewerActionType.WindowLevel, MedicalViewerMouseButtons.Left, MedicalViewerActionFlags.Active); 
      cell.SetAction(MedicalViewerActionType.Alpha, MedicalViewerMouseButtons.Middle, MedicalViewerActionFlags.Active); 
      cell.SetAction(MedicalViewerActionType.Offset, MedicalViewerMouseButtons.Right, MedicalViewerActionFlags.Active); 
      cell.SetAction(MedicalViewerActionType.Stack, MedicalViewerMouseButtons.Wheel, MedicalViewerActionFlags.Active); 
 
      // adjust some properties of the cell and add some tags. 
      _medicalViewer.Cells[0].SetTag(2, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.UserData, "EX. ID 230-36-5448"); 
      _medicalViewer.Cells[0].SetTag(4, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Frame); 
      _medicalViewer.Cells[0].SetTag(6, MedicalViewerTagAlignment.TopLeft, MedicalViewerTagType.Scale); 
      _medicalViewer.Cells[0].SetTag(2, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.WindowLevelData); 
      _medicalViewer.Cells[0].SetTag(1, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.FieldOfView); 
      _medicalViewer.Cells[0].SetTag(1, MedicalViewerTagAlignment.BottomLeft, MedicalViewerTagType.UserData, "Echo number = " + output.Stacks[0].EchoNumber.ToString()); 
 
      Controls.Add(_medicalViewer); 
      _medicalViewer.Dock = DockStyle.Fill; 
   } 
 
   void cell_FramesRequested(object sender, MedicalViewerRequestedFramesInformationEventArgs e) 
   { 
      MedicalViewerMultiCell cell = (MedicalViewerMultiCell)(sender); 
      RasterCodecs _codecs = new RasterCodecs(); 
      int i; 
      RasterImage image; 
      string fileName; 
 
      if (e.RequestedFramesIndexes.Length > 0) 
      { 
         fileName = (string)(_seriesManager.Stacks[0].Items[e.RequestedFramesIndexes[0]].Data); 
         image = _codecs.Load(fileName); 
      } 
      else 
         return; 
 
      for (i = 1; i < e.RequestedFramesIndexes.Length; i++) 
      { 
         fileName = (string)(_seriesManager.Stacks[0].Items[e.RequestedFramesIndexes[i]].Data); 
         image.AddPage(_codecs.Load(fileName)); 
      } 
 
      cell.SetRequestedImage(image, e.RequestedFramesIndexes, MedicalViewerSetImageOptions.Insert); 
   } 
 
   void  MedicalViewerSeriesManagerFrom_FormClosing(object sender, FormClosingEventArgs e) 
   { 
   } 
 
   // Find the study using the Study instance UID, and return it's DicomElement if the study is found 
   private DicomElement FindStudy(DicomDataSet ds, string studyInstanceUID) 
   { 
      // get the parent element. 
      DicomElement patientElement = ds.GetFirstKey(null, true); 
      DicomElement studyElement = null; 
      DicomElement studyInformationElement = null; 
      string studyID; 
 
      studyElement = ds.GetChildKey(patientElement); 
      studyElement = ds.GetChildElement(studyElement, true); 
 
      while (studyElement != null) 
      { 
         studyInformationElement = ds.FindFirstElement(studyElement, DicomTag.StudyInstanceUID, true); 
 
         if (studyInformationElement != null) 
         { 
            studyID = ds.GetConvertValue(studyInformationElement); 
 
            if (studyID == studyInstanceUID) 
               return studyInformationElement; 
         } 
 
         studyElement = ds.GetNextKey(studyElement, true); 
         studyElement = ds.GetChildElement(studyElement, true); 
      } 
      return null; 
   } 
 
   // Find the series using the Series instance UID, and return it's DicomElement if the series is found 
   private DicomElement FindSeries(DicomDataSet ds, DicomElement studyElement, string seriesInstanceUID) 
   { 
      DicomElement seriesElement = null; 
      DicomElement seriesInformationElement = null; 
      string seriesID; 
 
      seriesElement = ds.GetChildKey(studyElement); 
      seriesElement = ds.GetChildElement(seriesElement, true); 
 
      while (seriesElement != null) 
      { 
         seriesInformationElement = ds.FindFirstElement(seriesElement, 
                                                        DicomTag.SeriesInstanceUID, 
                                                        true); 
 
         if (seriesInformationElement != null) 
         { 
            seriesID = ds.GetConvertValue(seriesInformationElement); 
 
            if (seriesID == seriesInstanceUID) 
               return seriesInformationElement; 
         } 
 
         seriesElement = ds.GetNextKey(seriesElement, true); 
         seriesElement = ds.GetChildElement(seriesElement, true); 
      } 
      return null; 
   } 
 
   // return the first frame file name of the series. 
   private string GetFirstImageName(DicomDataSet ds, DicomElement seriesElement, string directoryPath, out DicomElement imageElement) 
   { 
      DicomElement imageIDElement = null; 
 
      imageElement = ds.GetChildKey(seriesElement); 
      imageElement = ds.GetChildElement(imageElement, true); 
 
      while (imageElement != null) 
      { 
         imageIDElement = ds.FindFirstElement(imageElement, 
                                            DicomTag.ReferencedFileID, 
                                            true); 
 
         if (imageIDElement != null) 
         { 
            return directoryPath + "\\" + ds.GetConvertValue(imageIDElement); 
         } 
 
 
      } 
 
      return ""; 
   } 
 
   // return the next frame file name of the series. 
   private string GetNextImageName(DicomDataSet ds, string directoryPath, ref DicomElement imageElement) 
   { 
      DicomElement nextImageElement = null; 
 
      imageElement = ds.GetNextKey(imageElement, true); 
      imageElement = ds.GetChildElement(imageElement, true); 
 
      while (imageElement != null) 
      { 
         nextImageElement = ds.FindFirstElement(imageElement, 
                                            DicomTag.ReferencedFileID, 
                                            true); 
 
         if (imageElement != null) 
         { 
            DicomElement echoElement = ds.FindFirstElement(imageElement, 
                                              DicomTag.EchoNumber, 
                                              true); 
 
            return directoryPath + "\\" + ds.GetConvertValue(nextImageElement); 
         } 
      } 
 
      return ""; 
   } 
 
 
   private bool AddImageToImageArray(DicomDataSet ds, int index, string imagePath, out int echoNumber) 
   { 
      echoNumber = -1; 
 
      MedicalViewerImageData imageData = new MedicalViewerImageData(); 
 
      patientElement = ds.FindFirstElement(null, 
                                           DicomTag.ImagePositionPatient, 
                                           true); 
 
      doubleArray = ds.GetDoubleValue(patientElement, 0, 3); 
 
      imageData.ImagePosition = Point3D.FromDoubleArray(doubleArray); 
      imageData.Data = imagePath; 
 
      imageData.EchoNumber = echoNumber; 
 
      patientElement = ds.FindFirstElement(null, 
                                          DicomTag.FrameOfReferenceUID, 
                                          true); 
 
      referenceUID = ds.GetConvertValue(patientElement); 
 
      imageData.FrameOfReferenceUID = referenceUID; 
 
      patientElement = ds.FindFirstElement(null, 
                                          DicomTag.ImageOrientationPatient, 
                                          true); 
 
      imageData.ImageOrientation = ds.GetConvertValue(patientElement); 
 
      patientElement = ds.FindFirstElement(null, 
                                           DicomTag.PixelSpacing, 
                                           true); 
 
      doubleArray = ds.GetDoubleValue(patientElement, 0, 2); 
      imageData.PixelSpacing = new Point2D((float)doubleArray[0], (float)doubleArray[1]); 
 
      _imageDataList.Add(imageData); 
 
      return true; 
 
   } 
 
   public MedicalViewerSeriesManager LoadJamesHead() 
   { 
      DicomEngine.Startup(); 
      DicomDataSet ds = new DicomDataSet(); 
      ds.Load(Path.Combine(LEAD_VARS.JamesCTDir , "DICOMDIR"), DicomDataSetLoadFlags.None); 
      string directoryPath = LEAD_VARS.JamesCTDir;///*LeadtoolsExamples.Common.ImagesPath.Path*/ Path.Combine(LEAD_VARS.ImagesDir, @"\Miller James-CT Head full Study\"); 
 
      string studyInstanceUID = "1.3.12.2.1107.5.1.4.50772.30000009122208074910900000022"; 
      string seriesInstanceUID = "1.3.12.2.1107.5.1.4.50772.30000009122208215356200001997"; 
 
      _studyElement = FindStudy(ds, studyInstanceUID); 
      _seriesElement = FindSeries(ds, _studyElement, seriesInstanceUID); 
 
      int count = 256; 
 
      _seriesManager = new MedicalViewerSeriesManager(); 
      _imageDataList = new List<MedicalViewerImageData>(); 
 
 
      DicomDataSet dicomDataSet; 
 
      int imageIndex; 
      string imagePath; 
      int echoNumber = 0; 
      imagePath = GetFirstImageName(ds, _seriesElement, directoryPath, out imageElement); 
      for (imageIndex = 0; imageIndex < count; imageIndex++) 
      { 
         try 
         { 
            dicomDataSet = new DicomDataSet(); 
            dicomDataSet.Load(imagePath, DicomDataSetLoadFlags.None); 
            AddImageToImageArray(dicomDataSet, imageIndex, imagePath, out echoNumber); 
            dicomDataSet.Dispose(); 
 
            imagePath = GetNextImageName(ds, directoryPath, ref imageElement); 
 
         } 
         catch (System.Exception exception) 
         { 
            System.Diagnostics.Debug.Assert(false, exception.Message); 
            throw; 
         } 
      } 
 
 
 
      _seriesManager.Sort(_imageDataList); 
      DicomEngine.Shutdown(); 
 
      return _seriesManager; 
   } 
 
 
   public MedicalViewerSeriesManager LoadJamesHead512() 
   { 
      DicomEngine.Startup(); 
      DicomDataSet ds = new DicomDataSet(); 
      ds.Load(Path.Combine(LEAD_VARS.JamesCTDir, "DICOMDIR"), DicomDataSetLoadFlags.None); 
      string directoryPath = LEAD_VARS.JamesCTDir; 
 
      string studyInstanceUID = "1.3.12.2.1107.5.1.4.50772.30000009122208074910900000022"; 
      string seriesInstanceUID = "1.3.12.2.1107.5.1.4.50772.30000009122208215356200002254"; 
 
      _studyElement = FindStudy(ds, studyInstanceUID); 
      _seriesElement = FindSeries(ds, _studyElement, seriesInstanceUID); 
 
      int count = 512; 
 
      _seriesManager512 = new MedicalViewerSeriesManager(); 
      _imageDataList = new List<MedicalViewerImageData>(); 
 
 
      DicomDataSet dicomDataSet; 
 
      int imageIndex; 
      string imagePath; 
      int echoNumber = 0; 
      imagePath = GetFirstImageName(ds, _seriesElement, directoryPath, out imageElement); 
      for (imageIndex = 0; imageIndex < count; imageIndex++) 
      { 
         try 
         { 
            dicomDataSet = new DicomDataSet(); 
            dicomDataSet.Load(imagePath, DicomDataSetLoadFlags.None); 
            AddImageToImageArray(dicomDataSet, imageIndex, imagePath, out echoNumber); 
            dicomDataSet.Dispose(); 
 
            imagePath = GetNextImageName(ds, directoryPath, ref imageElement); 
 
         } 
         catch (System.Exception exception) 
         { 
            System.Diagnostics.Debug.Assert(false, exception.Message); 
            throw; 
         } 
      } 
 
 
 
      _seriesManager512.Sort(_imageDataList); 
      DicomEngine.Shutdown(); 
 
      return _seriesManager512; 
   } 
 
} 
 
 
 
MedicalViewerSeriesManagerFrom GetMedicalControlForReverseExample() 
{ 
   MedicalViewerSeriesManagerFrom form = new MedicalViewerSeriesManagerFrom(); 
   MedicalViewerSeriesManager output = form.LoadJamesHead(); 
 
   return new MedicalViewerSeriesManagerFrom(output); 
} 
//  
public void MedicalViewerSeriesManagerLoadingExample() 
{ 
   MedicalViewerSeriesManagerFrom myForm = GetMedicalControlForReverseExample(); 
   myForm.ShowDialog(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS22\Resources\Images"; 
} 
Requirements

Target Platforms

Help Version 22.0.2023.2.5
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.MedicalViewer Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.